//
//  UIRefreshView.swift
//  刷新控件
//
//  Created by 伍禄林 on 16/10/18.
//  Copyright © 2016年 伍禄林. All rights reserved.
//

import UIKit

class UIRefreshView: UIControl {
    
    
    //UIView 封装的旋转动画
    //默认顺势针方向  就近原则（所以减 0.001）
    var refreshStatus: CZRefreshStatus = .Normal{
        
        didSet {
            
            switch refreshStatus {
            case .Normal:
                
                tipIcon.isHidden = false
                idicator.stopAnimating()
                tipLabel.text = "继续使劲拉"
                UIView.animate(withDuration: 0.25, animations: {
                    self.tipIcon.transform = CGAffineTransform.identity
                })
                
            case .Pulling:
                tipLabel.text = "松手刷新"
                
                UIView.animate(withDuration: 0.25, animations: { 
                    self.tipIcon.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI - 0.001))
                })
                
            case .Willrefresh:
                
                tipLabel.text = "正在刷新。。。"
                tipIcon.isHidden  = true
                idicator.startAnimating()
                
            }
        }
        
    }
    
    @IBOutlet weak var idicator: UIActivityIndicatorView!
    
    @IBOutlet weak var tipIcon: UIImageView!
    
    @IBOutlet weak var tipLabel: UILabel!
    
    
    class func refreshView() -> UIRefreshView {
        
        let nib = UINib(nibName: "CZRefreshView", bundle: nil)

        return nib.instantiate(withOwner: nil, options: nil)[0] as! UIRefreshView
    }
}
